package cn.e4j.modular.api.controller;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.StringUtils;
import cn.e4j.core.util.ToolUtil;
import cn.e4j.modular.api.service.XchbService;
import cn.e4j.modular.api.upload.ImgUtils;
import cn.e4j.modular.api.util.ControllerSupport;
import cn.e4j.modular.crm.entity.*;
import cn.e4j.modular.system.entity.SysUser;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by wenchuan.li on 2018/04/16.
 */
@RestController
@RequestMapping("api/wx/xchb")
public class XchbController extends ControllerSupport {

    @Autowired
    private XchbService xchbService;

    /**
     * 获取现场回报列表
     *
     * @return
     */
    @RequestMapping(value = "getXchbList")
    public ResponseData getServiceOrderList(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        List<Dto> list = new ArrayList<>();
        SysUser currentUser = wapper.getSysUser();
        try {
            inDto.put("MMRFENT", inDto.getString("ENT"));
            inDto.put("MMRF007", currentUser.getID());//员工编号

            list = xchbService.getListPage(wapper.getPage(),inDto);
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS(list);
    }

    /**
     * 现场回报保存
     * 问题处理方案用mmrg102，处理结果用mmrg100 1.处理完成、2.暂缓处理、3.未解决问题
     * <p>
     * 配件三包到期日期mmrh007: 用服务派工日期 + 配件中的保固月数和天数计算得到
     *
     * @return
     */
    @RequestMapping(value = "xchbSave")
    public ResponseData xchbSave(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        SysUser currentUser = wapper.getSysUser();
        MmrgT mmrg_tPO = new MmrgT();
        MmrdT mmrd_tPO = new MmrdT();
        List<MmrhT> Mmrh_tPOs = new ArrayList<>();
        List<MmriT> Mmri_tPOs = new ArrayList<>();
        List<MmrqT> mmrq_tPOS = new ArrayList<>();
        ResponseData data = new ResponseData();
        try {

            JSONObject inJson = JSONObject.parseObject(inDto.getString("obj"));

            data = ImgUtils.base64ToImg(inJson.getString("signature"));
            if (data.getCode() == 200) {
                String url = (String) data.getData();
                
                inDto.put("MMRFENT", inDto.getString("ENT"));
                inDto.put("MMRF007", currentUser.getID());//员工编号

                mmrg_tPO.setMMRGENT(currentUser.getDBC());
                mmrg_tPO.setMMRGDOCNO("WXHB" + DateUtils.getDate("yyyyMMddHHmmssSSS"));
                mmrg_tPO.setMMRGSEQ(BigDecimal.ONE);
                //mmrg_tPO.setMMRGDOCDATE(new Date());
                mmrg_tPO.setMMRGDOCDATE(LocalDate.now());
                mmrg_tPO.setMMRG001(inJson.getString("MMRFDOCNO"));
                mmrg_tPO.setMMRG002(inJson.getBigDecimal("MMRFSEQ"));
                mmrg_tPO.setMMRG003(inJson.getString("MMRF003"));
                mmrg_tPO.setMMRG004(inJson.getString("MMRF004"));
                mmrg_tPO.setMMRG005(currentUser.getID());
                Date mmrf009 = inJson.getDate("MMRF009");
                if (mmrf009 != null) {
                    //mmrg_tPO.setMMRG006(mmrf009);
                    mmrg_tPO.setMMRG006(mmrf009.toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
                }else{
                    //mmrg_tPO.setMMRG006(new Date());
                    mmrg_tPO.setMMRG006(new Date().toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
                }
                //mmrg_tPO.setMMRG007(inJson.getDate("MMRG007"));
                mmrg_tPO.setMMRG007(inJson.getDate("MMRG007").toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
                mmrg_tPO.setMMRG008(inJson.getString("MMRG008"));
                mmrg_tPO.setMMRG009(inJson.getString("MMRG009"));// 图片地址
                mmrg_tPO.setMMRG010(inJson.getString("MMRG010"));
                mmrg_tPO.setMMRG011(inJson.getString("MMRG011"));
                
                if (ToolUtil.isNotEmpty(url)){
                    mmrg_tPO.setMMRG012(url); // 签名地址
                }
                mmrg_tPO.setMMRG013(inJson.getString("MMRG013"));
                mmrg_tPO.setMMRG100(inJson.getString("MMRG100"));
                mmrg_tPO.setMMRG102(inJson.getString("MMRG102"));
                mmrg_tPO.setMMRGACTI("Y");
                //mmrg_tPO.setMMRGDATE(new Date());
                mmrg_tPO.setMMRGDATE(LocalDate.now());
                mmrg_tPO.setMMRGTIME(DateUtils.getTime());
                mmrg_tPO.setMMRGUSER(currentUser.getID());

                // 修改 mmrd表 状态为报工
                mmrd_tPO.setMMRDENT(currentUser.getDBC());
                mmrd_tPO.setMMRDSEQ(inJson.getBigDecimal("MMRDSEQ"));
                mmrd_tPO.setMMRDDOCNO(inJson.getString("MMRF001"));
                mmrd_tPO.setMMRD020("4");

                JSONArray mmrhJA = inJson.getJSONArray("pjlist");

                for (int i = 0, j = mmrhJA.size(); i < j; i++) {
                    JSONObject item = mmrhJA.getJSONObject(i);
                    MmrhT mmrh = new MmrhT();
                    mmrh.setMMRHENT(currentUser.getDBC());
                    mmrh.setMMRHDOCNO(mmrg_tPO.getMMRGDOCNO());
                    mmrh.setMMRHSEQ(BigDecimal.valueOf(i+1));
                    mmrh.setMMRH001(item.getString("MMRH001"));
                    mmrh.setMMRH002(item.getString("MMRH002"));
                    mmrh.setMMRH003(item.getString("MMRS004"));
                    mmrh.setMMRH004(item.getBigDecimal("MMRH004"));
                    mmrh.setMMRH005(item.getBigDecimal("MMRS009"));
                    mmrh.setMMRH006(item.getString("MMRH002") == "1" ? mmrh.getMMRH004().multiply(mmrh.getMMRH005()) : BigDecimal.ZERO);

                    QueryWrapper<GwamT> gwamTQueryWrapper = new QueryWrapper<>();
                    gwamTQueryWrapper.eq("GWAMENT", inDto.getString("ENT")).eq("GWAM001", item.getString("MMRS004"));
                    GwamT gwam_tPO = new GwamT().selectOne(gwamTQueryWrapper);
                    if (gwam_tPO != null && ToolUtil.isNum(gwam_tPO.getGWAM012()) && ToolUtil.isNum(gwam_tPO.getGWAM013())) {
                        /*mmrh.setMMRH007(DateUtils.addDays(new Date(),
                                Integer.parseInt(gwam_tPO.getGWAM012())*30+Integer.parseInt(gwam_tPO.getGWAM013()))); //配件三包到期日期*/
                        mmrh.setMMRH007(DateUtils.addDays(new Date(),
                                Integer.parseInt(gwam_tPO.getGWAM012())*30+Integer.parseInt(gwam_tPO.getGWAM013())).toInstant().atZone(ZoneId.systemDefault()).toLocalDate()); //配件三包到期日期
                    }
                    mmrh.setMMRH008(item.getString("MMRS010"));
                    mmrh.setMMRH009(item.getString("MMRH009"));
                    mmrh.setMMRHACTI("Y");
                    mmrh.setMMRHDATE(LocalDate.now());
                    mmrh.setMMRHTIME(DateUtils.getTime());
                    mmrh.setMMRHUSER(currentUser.getID());
                    Mmrh_tPOs.add(mmrh);
                }

                JSONArray expenses = inJson.getJSONArray("expensesList");

                for (int i = 0, j = expenses.size(); i < j; i++) {
                    JSONObject item = expenses.getJSONObject(i);
                    MmriT mmri = new MmriT();
                    mmri.setMMRIENT(currentUser.getDBC());
                    mmri.setMMRIDOCNO(mmrg_tPO.getMMRGDOCNO());
                    mmri.setMMRISEQ(BigDecimal.valueOf(i+1));
                    mmri.setMMRI001(item.getString("MMRI001"));
                    mmri.setMMRI002(item.getBigDecimal("MMRI002"));
                    mmri.setMMRI003(item.getBigDecimal("MMRI003"));
                    mmri.setMMRIACTI("Y");
                    mmri.setMMRIDATE(LocalDate.now());
                    mmri.setMMRITIME(DateUtils.getTime());
                    mmri.setMMRIUSER(currentUser.getID());
                    Mmri_tPOs.add(mmri);
                }

                String MMRD013s = inJson.getString("MMRD013");
                String[] MMRD013Array = MMRD013s.split(",");

                int seq = 1;
                for(String MMRD013:MMRD013Array){
                    if (StringUtils.isNotEmpty(MMRD013)){
                        MmrqT mmrq_tPO = new MmrqT();
                        mmrq_tPO.setMMRQDOCNO(mmrg_tPO.getMMRGDOCNO());
                        mmrq_tPO.setMMRQENT(currentUser.getDBC());
                        mmrq_tPO.setMMRQSEQ(seq);
                        mmrq_tPO.setMMRQ001(MMRD013);

                        mmrq_tPO.setMMRQACTI("Y");
                        mmrq_tPO.setMMRQDATE(LocalDate.now());
                        mmrq_tPO.setMMRQTIME(DateUtils.getTime());
                        mmrq_tPO.setMMRQUSER(currentUser.getID());
                        mmrq_tPO.setMMRQGRUP(currentUser.getORG_ID());
                        mmrq_tPO.setMMRQORIG(currentUser.getORG_ID());
                        mmrq_tPO.setMMRQORIU(currentUser.getID());

                        mmrq_tPOS.add(mmrq_tPO);
                        seq ++;
                    }
                }
                
                
                xchbService.xchbSave(mmrg_tPO,Mmrh_tPOs,Mmri_tPOs,mmrd_tPO,mmrq_tPOS);
            }else
                return data;

        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS;
    }


}
